﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using TaoPortal.Common.Utility;

namespace TaoPortal.Common
{
    public class SysInit
    {
        public void InitParamValue()
        {
            Dictionary<string, Dictionary<string, string>> ParamValues = new Dictionary<string, Dictionary<string, string>>();
            //系统内容类别
            AddParamItem(ParamValues, typeof(SysEnum.SysClassType));
            //文章状态
            AddParamItem(ParamValues, typeof(SysEnum.ArticleStatus));
            //产品状态
            AddParamItem(ParamValues, typeof(SysEnum.ProductStatus));
            //角色类型
            AddParamItem(ParamValues, typeof(SysEnum.RoleType));
            HttpContextCache.Add(PublicKey.PARAMVALUECACHE_KEY, ParamValues);
        }

        private void AddParamItem(Dictionary<string, Dictionary<string, string>> ParamValues, Type t)
        {
            Dictionary<string,string> item = new Dictionary<string,string>();
            var fields = t.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (var f in fields)
            {
                var attr = f.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attr.Length > 0)
                {
                    string code = Convert.ToInt32(Enum.Parse(t, f.Name)).ToString();
                    string value = ((DescriptionAttribute)attr[0]).Description;
                    item.Add(code, value);
                }
            }
            ParamValues.Add(t.Name, item);
        }
    }
}
